/**
 * Created by yaoyujie on 2016/9/19.
 */
/**
 * 分页查询 插件
 * @param opt
 * opt.url          ajax请求路径
 * opt.formId       ajax请求数据
 * opt.rows         一页显示几条数据    默认值:20
 * opt.columns      table显示的字段
 * opt.searchBtnId  查询button的id
 * opt.check        显示checkbox      默认false(如果为true,请增加rowId的参数)
 * opt.rowId        checkbox value的值
 */
$.fn.dataTableHelper = function (opt) {
    var _self = this;
    var options = {
        processing: true, //打开数据加载时的等待效果
        autoWidth: false,
        serverSide: true, //打开后台分页
        processing: true,
        lengthChange: false,
        ordering: false,
        searching: false,
        pageLength: 50,
        ajax: function (data, callback, settings) {
            $(_self).find('.select-all').prop('checked', false);

            if (opt.data) {
                data = opt.data;
                $.ajax({
                    type: "POST",
                    url: opt.url,
                    data: data,
                    success: function (data) {
                        callback(data);
                    }
                });
            } else {
                data = settings.oAjaxData;

                if (!opt.formId) {
                    opt.formId = "search-form";
                }
                data = $.extend(true, data, $('#' + opt.formId).serializeObject());
                $.ajax({
                    type: "POST",
                    url: opt.url,
                    data: data,
                    dataType: "json",
                    success: function (data) {
                        callback(data);
                    }
                });
            }
            //console.log(data);


        },
        language: {
            "sProcessing": "处理中...",
            "sLengthMenu": "显示 _MENU_ 项结果",
            "sZeroRecords": "没有匹配结果",
            "sInfo": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
            "sInfoEmpty": "显示第 0 至 0 项结果，共 0 项",
            "sInfoFiltered": "(由 _MAX_ 项结果过滤)",
            "sInfoPostFix": "",
            "sSearch": "搜索:",
            "sUrl": "",
            "sEmptyTable": "没有匹配结果",
            "sLoadingRecords": "载入中...",
            "sInfoThousands": ",",
            "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "上页",
                "sNext": "下页",
                "sLast": "末页"
            },
            "oAria": {
                "sSortAscending": ": 以升序排列此列",
                "sSortDescending": ": 以降序排列此列"
            }
        }
    };

    if (opt.check && opt.check === true) {
        $(_self).find('thead tr').prepend('<th class="text-center"><input class="select-all" type="checkbox"/></th>');

        opt.columns.splice(0, 0, {
            sClass: 'text-center',
            data: opt.rowId,
            render: function (data, type, row, meta) {
                return '<input type="checkbox" class="checkbox-cell" value="' + data + '">';
            }
        });
        $(_self).find('.select-all').click(function () {
            $(_self).find('.checkbox-cell').prop('checked', $(this).prop('checked'));
        });
    }
    options = $.extend(options, opt);
    var datatable = $(this).DataTable(options);

    if (!opt.searchBtnId) {
        opt.searchBtnId = "search-btn";
    }

    $('#' + opt.searchBtnId).click(function () {
        //datatable.settings()[0].ajax.data = $('#'+formId).serializeObject();
        datatable.ajax.reload();
    });
    /**获取当前选中的checkbox的value*/
    datatable.getSelectArray = function () {
        var arr = new Array();
        $(_self).find('.checkbox-cell:checked').each(function () {
            arr.push($(this).val());
        });
        return arr;
    }

    return datatable;
};

